package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.Map;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.entity.Files;


public class FileDao {

	/**
	 * 保存附件信息
	 * 
	 * @param attachs
	 * @return
	 * @throws SQLException
	 */
	public Long savFile(Files file) throws SQLException {
		String insertSql = "insert into `file` ( `model`, `file_size`, `file_name`, `file_path`) values (?,?,?,?);";
		// 如果记录插入成功，获取最新记录ID
		if (Jdbc.getInstace().executeUpdate(insertSql, file.getModel(), file.getFileSize(), file.getFileName(),
				file.getFilePath())) {
			String sql = "select file_id id from file order by file_id desc limit 1";
			Map<String, Object> map = Jdbc.getInstace().executeQuery(sql);
			if (map != null) {
				return Long.parseLong(map.get("id").toString());
			}
		}
		return null;
	}
	/**
	 * 关联附件
	 * 
	 * @param mianShiId
	 * @param attchs
	 * @throws SQLException
	 */
	public void guanLian(Long mianShiId, String[] fiel) throws SQLException {
		String sql = "update file set recod = ? where file_id = ? ";
		for (String aid : fiel) {
			Jdbc.getInstace().executeUpdate(sql, mianShiId, aid);
		}
}
}
